package com.ruoyi.quartz.task;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.management.paystatuslogs.service.PayStatusLogsService;
import com.ruoyi.passenger.order.service.ITOrderService;
import com.ruoyi.passenger.pay.service.IPayService;
import com.ruoyi.passenger.pay.vo.PayLogsVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 更新订单支付状态
 * 
 * @author lss
 */
@Component("updateorderpaystatustask")
public class UpdateOrderPayStatusTask
{
    @Autowired
    private IPayService payService;
    @Autowired
    private ITOrderService orderService;
    @Autowired
    private PayStatusLogsService payStatusLogsService;

    /**
     * 更新订单支付状态
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateOrderPayStatus(){
        List<String> payOrderIdList = orderService.getPayOrderId();
        List<Map> payList = Lists.newArrayList();
        List<PayLogsVO> logsList = new ArrayList();
        for (String payOrderId : payOrderIdList) {
            PayLogsVO vo = payService.getPayResultForTask(payOrderId);
            vo.setPayOrderId(payOrderId);
            logsList.add(vo);
            Map<String,String> updateMap = Maps.newHashMap();
            updateMap.put("payOrderId",payOrderId);
            updateMap.put("statusCode",vo.getPayResult());
            payList.add(updateMap);
        }
        if (null != payList && !payList.isEmpty())
        {
            orderService.updateBatchPayStatus(payList);
        }
        if(!CollectionUtils.isEmpty(logsList)){
            payStatusLogsService.saveLogs(logsList);
        }
    }
}
